"""Helper functions

Consists of functions to typically be used within templates, but also
available to Controllers. This module is available to templates as 'h'.
"""
# Import helpers as desired, or define your own, ie:
#from webhelpers.html.tags import checkbox, password

from datetime import datetime, timedelta

def make_stage_url(tournament, stages, this_stage):
    return tournament + '/' + '/'.join(stages + [this_stage])

def make_match_url(tournament, stages, this_stage, team_one, team_two):
    stage_url = make_stage_url(tournament, stages, this_stage)
    return stage_url + '/' + team_one + '-v-' + team_two

def make_set_url(tournament, stages, team_one, team_two, set_number):
    stage_path = stages[:-1]
    this_stage = stages[-1]
    match_url = make_match_url(tournament, stage_path, this_stage, team_one, team_two)
    return match_url + '/Set' + str(set_number)

def render_date(date, format='%b %d, %Y'):
    if not date:
        return ''
    else:
        return date.strftime(format)

def render_date_range(date_one, date_two, format='%b %d, %Y'):
    if date_one == date_two:
        return render_date(date_one)
    else:
        return date_one.strftime(format) + ' - ' + date_two.strftime(format)
